import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        city: '',
        cid: '',
        login: false,
        userName: '',
        uid: '',
    },
    mutations: {
        setCity(state, city) {
            state.city = city;
        },
        setCid(state, cid) {
            state.cid = cid
        },
        setUid(state, uid) {
            if (state.login) {
                state.uid = uid;
            } else {
                state.uid = ''
            }
            console.log(state.uid)

        },
        setUserName(state, userName) {

            state.userName = userName;
            console.log(userName)
            if (userName) { state.login = true; } else {
                state.login = false
            }

        },
    },
    actions: {
        plogin(context, str) {
            axios.get('/v1/house/city', { params: { city: str } }).then(res => {
                // console.log(res)
                context.commit('setCity', res.data[0].cname);
                context.commit('setCid', res.data[0].cid);
            })
        },
    },
    modules: {}
})